const { getUserInfo } = require("../service/user/index");
const { userFormateError, userAlreadyExit } = require("../constant/err.type");
const userValidator = async (ctx, next) => {
  const { user_name, password } = ctx.request.body;
  if (!user_name || !password) {
    ctx.app.emit("error", userFormateError, ctx);
    return;
  }
  await next();
};

const usernameValidator = async (ctx, next) => {
  const { user_name } = ctx.request.body;
  if (await getUserInfo({ user_name })) {
    ctx.app.emit("error", userAlreadyExit, ctx);
    return;
  }
  await next();
};
module.exports = {
  userValidator,
  usernameValidator,
};
